﻿using System;
using System.Data.SqlClient;
using BikeBouncer.BusinessLayer.Entities;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader ApiKey_Get(String text)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@Text", text);

			return (SqlDataReader)db.ExecuteReader("upApiKey_Get");
		}

		#endregion

		#region Insert and update

		public static void ApiKey_Insert(ApiKey key)
		{
			using (DataHandler db = new DataHandler())
			{
				key.Hashcode = new Guid().ToString();

				db.AddParameter("@UserId", key.User.Id);
				db.AddParameter("@Hashcode", key.Hashcode);
				db.AddParameter("@FullAccess", key.FullAccess);
				db.AddParameter("@Active", key.Active);

				key.Id = Convert.ToInt32(db.ExecuteScalar("upApiKey_Insert"));
			}
		}

		public static void ApiKey_UpdateLoginDate(ApiKey key)
		{
			if (key.Id < 1)
			{
				return;
			}

			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@ApiKeyId", key.Id);

				db.ExecuteScalar("upApiKey_UpdateLoginDate");
			}
		}

		#endregion
	}
}